from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from metadataobject import *

class MetadataObjectsModel(QAbstractTableModel): 
  def __init__(self, datain=[], parent=None, *args): 
    """ datain: a list of metadata objects
    """
    QAbstractListModel.__init__(self, parent, *args) 
    self.listdata = datain

  def rowCount(self, parent=QModelIndex()): 
    return len(self.listdata) 

  def columnCount(self, parent=QModelIndex()):
    if parent.isValid():
      return 0
    else:
      return 2
    
  def headerData (self,section,orientation, role=Qt.DisplayRole):
    result = QVariant(); 
    if orientation==Qt.Horizontal and role == Qt.DisplayRole:
      if section==0:
        result = QVariant("meta")
      if section==1:
        result = QVariant("state")
      return result
    
  
  def data(self, index, role):
    result = QVariant(); 
    if index.isValid() and role == Qt.DisplayRole:
      if index.column()==0:
        result = QVariant(self.listdata[index.row()].meta)
    if index.isValid() and role == Qt.DecorationRole:
      if index.column()==0:
        result = QVariant(QIcon(":/plugins/postmeta/icons/mimetypes/"+
        self.listdata[index.row()].mimeType.replace("/","-") +
        ".png"))
      if index.column()==1:
        metaDataObject = self.listdata[index.row()]
        if MetaDataObject.STATE_INSERT==metaDataObject.state:
          result = QVariant(QIcon(":/plugins/postmeta/icons/edit_add.png"))
        elif MetaDataObject.STATE_DELETE == metaDataObject.state:
          result = QVariant(QIcon(":/plugins/postmeta/icons/edit_delete.png"))
        else:
          result = QVariant(QIcon(":/plugins/postmeta/icons/ok.png"))
    return result
